package com.imagination.applet.infrastructure.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.security.PrivilegedAction;
import java.time.LocalDateTime;


@Service
@RequiredArgsConstructor(onConstructor = @__({@Autowired}))
public class AccessTokenUtils {

    @Value(value = "${payment.wechat.appId}")
    private String appId;

    @Value(value = "${payment.wechat.appSecret}")
    private String secret;


    private LocalDateTime dueTime;

    private String accessToken;

    private final String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token";

    public String getToken(){
        if(dueTime!=null&&dueTime.isBefore(LocalDateTime.now())){
            return accessToken;
        }
        HttpRequest get = HttpUtil.createGet(tokenUrl+"?grant_type=client_credential&appid="+appId+"&secret="+secret);
        HttpResponse execute = get.execute();
        JSONObject entries = JSONUtil.parseObj(execute.body());
        dueTime = LocalDateTime.now().plusMinutes(90L);
        accessToken = entries.getStr("access_token");
        return accessToken;
    }

    /**
     * 获取分享码
     * @param userId
     * @return
     */
    public byte[] getwxacodeunlimit(Long userId){
        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+getToken();
        JSONObject reqBody = new JSONObject();
        reqBody.putOpt("page","pages/mine/index");
        reqBody.putOpt("scene","userId="+userId);
        reqBody.putOpt("check_path",true);
        reqBody.putOpt("env_version","release");
//        reqBody.putOpt("env_version","develop");
        HttpRequest post = HttpUtil.createPost(url);
        post.body(reqBody.toString());
        HttpResponse execute = post.execute();
        if(execute.header("Content-Type").equals("application/json; encoding=utf-8")){
            dueTime=null;
            return getwxacodeunlimit(userId);
        }

        return execute.bodyBytes();
    }

    public byte[] getwxacodeunlimit(String scene,String page,String version){
        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+getToken();
        JSONObject reqBody = new JSONObject();
        reqBody.putOpt("page",page);
        reqBody.putOpt("scene","source="+scene);
        reqBody.putOpt("check_path",true);
        reqBody.putOpt("env_version",version);
        reqBody.putOpt("is_hyaline",true);
//        reqBody.putOpt("env_version","develop");
        HttpRequest post = HttpUtil.createPost(url);
        post.body(reqBody.toString());
        HttpResponse execute = post.execute();
        if(execute.header("Content-Type").equals("application/json; encoding=utf-8")){
            dueTime=null;
            return getwxacodeunlimit(scene,page,version);
        }

        return execute.bodyBytes();
    }


}
